<#import "/WEB-INF/sys/js_css.html" as js>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>权限管理</title>
</head>
<body>
	<!-- 工具条 -->
	<div id="tools" style="padding:5px;height:auto">
		<a href="javascript:void(0)" id="btn_top_add" class="easyui-linkbutton" iconCls="icon-add" plain="true">增加顶级权限</a>
	</div>
	<table id="datagrid" class="easyui-treegrid" data-options="singleSelect:true,fit:true,fitColumns:true,pagination:true,
		url:'${ctx}/sys/menu/search',toolbar:'#tools',idField:'id',treeField:'authname',rownumbers : true,
		toolbar:'tools'">
	    <thead>
			<tr>
				<th data-options="field:'id',hidden:true">id</th><!-- hidden -->
				<th data-options="field:'entityid',hidden:true">entityid</th><!-- hidden -->
				<th data-options="field:'pnumber',hidden:true">父级编号</th><!-- hidden -->
				<th data-options="field:'createUser',hidden:true">创建人编号</th><!-- hidden ,formatter:formatterAction -->
				<th data-options="field:'action',align:'center',width:10,formatter:formatterAction">操作</th>
				<th data-options="field:'number',width:10">编号</th>
				<th data-options="field:'authname',width:30">权限名</th>
				<th data-options="field:'authurl',width:15">访问路径</th><!-- hidden -->
				<th data-options="field:'createUserName',align:'center',width:10">创建人</th>
				<th data-options="field:'createTime',align:'center',width:15">创建时间</th>
			</tr>
		</thead>
    </table>
    
    <!-- 添加权限信息div -->
	<div id="divpadd" class="easyui-window" title="添加权限信息" data-options="modal:true,collapsible:false,closed:true,minimizable:false,
		maximizable:false">
		<form id="topaddform" method="post">
			<input type="hidden" name="id" id="addid" value="" />
			<input type="hidden" name="pnumber" id="addpnumber" value="" />
			<input type="hidden" name="number" id="addnumber" value="" />
			<table class="table table-hover table-condensed">
				<tr>
					<td>权限名称：&nbsp;&nbsp;&nbsp;&nbsp;<input id="addauthname" name="authname" type="text" class="easyui-validatebox" value="" /></td>
				</tr>
				<tr>
					<td>访问路径：&nbsp;&nbsp;&nbsp;&nbsp;<input name="authurl" id="addauthurl" type="text" value="" /></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><a href="javascript:void(0)" class="easyui-linkbutton" id="btn_window_top_add" iconCls="icon-save" plain="true">保存</a></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
<@js.easyui />
<script type="text/javascript">
jQuery(document).ready(function() {
	//添加顶级权限信息
	jQuery("#btn_top_add").click(function(){
		jQuery("#divpadd").window("open");
		jQuery("#addid").val("");
		jQuery("#addpnumber").val("");
		jQuery("#addnumber").val("");
		jQuery("#addauthname").val("");
		jQuery("#addauthurl").val("");
	});
	jQuery("#btn_window_top_add").click(function(){
		jQuery("#topaddform").form({
			url:"${ctx}/sys/menu/add",
			onSubmit:function(){
				if(!validate())
					return false;
				var servervali = false;
				jQuery.ajax({
		   			async:false,
		   			type:"post",
		   			dataType:"json",
		   			data:{authname:jQuery("#addauthname").val(),sort:jQuery("#addsort").val(),
		   				authurl:jQuery("#addauthurl").val(),pnumber:jQuery("#addpnumber").val(),
		   				id:jQuery("#addid").val()},
		   			url:"${ctx}/sys/menu/validate",
		   			success:function(date){
		   				if(!date.success){
		   					jQuery.messager.alert("信息",date.message,"error");
		   				}else
		   					servervali = true;
		   			}
		   		});
				return servervali;
			},
			success:function(data){
				if(!data){
					jQuery.messager.alert("信息","保存失败！","error");
					return;
				}
				jQuery.messager.alert("信息","保存成功！","info",function(){
					jQuery("#divpadd").window("close");
				});
				jQuery("#datagrid").treegrid("reload");
			}
		});
		//提交表单
		jQuery('#topaddform').submit();
	});
});
//验证表单信息
function validate(){
	if(jQuery("#addauthname").val()==""){
		jQuery.messager.alert("信息","请填写权限名称！","info");
		return false;
	}
	return true;
}
//操作单元格信息
function formatterAction(value,row,index){
	return "<img title='添加子权限' onclick='addChildfun(\""+row.number+"\")' src='${ctx}/js/easyui/themes/icons/edit_add.png' />&nbsp;&nbsp;"+
	"<img title='更改此权限' onclick='editfun(\""+row.entityid+"\")' src='${ctx}/js/easyui/themes/icons/pencil.png' />&nbsp;&nbsp;"+
	"<img title='删除此权限' onclick='deletefun(\""+row.entityid+"\")' src='${ctx}/js/easyui/themes/icons/edit_remove.png' />";
}
//修改权限信息
function editfun(id){
	jQuery.ajax({
		async:false,
		type:"post",
		dataType:"json",
		data:{id:id},
		url:"${ctx}/sys/menu/bean",
		success:function(date){
			if(!date.success){
				jQuery.messager.alert("信息",date.message,"error");
				return;
			}
			var bean = date.bean;
			jQuery("#divpadd").window("open");
			jQuery("#topaddform").form("clear");
			
			jQuery("#addid").val(bean.id);
			jQuery("#addpnumber").val(bean.pnumber);
			jQuery("#addnumber").val(bean.number);
			jQuery("#addauthname").val(bean.authname);
			jQuery("#addauthurl").val(bean.authurl);
		}
	});
}
//添加子权限
function addChildfun(number){
	jQuery("#divpadd").window("open");
	jQuery("#topaddform").form("clear");
	
	jQuery("#addid").val("");
	jQuery("#addpnumber").val("");
	jQuery("#addnumber").val("");
	jQuery("#addauthname").val("");
	jQuery("#authurl").val("");
	
	jQuery("#addpnumber").val(number);
}
function deletefun(id){
	jQuery.messager.confirm("信息","您确定要删除此权限？",function(arg){
		if(!arg)
			return;
		jQuery.ajax({
			async:false,
			type:"post",
			dataType:"json",
			data:{id:id},
			url:"${ctx}/sys/menu/delete",
			success:function(date){
				if(!date.success){
					jQuery.messager.alert("信息",date.message,"error");
					return;
				}
				//刷新页面
				jQuery("#datagrid").treegrid("reload");
			}
		});
	});
}
</script>